package com.qen.yanggao

import cn.hutool.core.util.StrUtil
import cn.hutool.db.Db
import cn.hutool.poi.excel.ExcelUtil

const val OutPath = "/Users/a1/Documents/Git/Kotlin/Eleven/out/yanggao/"

val DataType = mutableMapOf<String, String>(
    "blob" to "二进制blob",
    "mediumblob" to "二进制blob",
    "varbinary" to "二进制blob",
    "longblob" to "二进制blob",
    "decimal" to "浮点型F",
    "float" to "浮点型F",
    "double" to "浮点型F",
    "datetime" to "日期时间型T",
    "time" to "日期时间型T",
    "date" to "日期型D",
    "timestamp" to "数值型N",
    "mediumint" to "数值型N",
    "bigint" to "整型I",
    "int" to "整型I",
    "tinyint" to "整型I",
    "smallint" to "整型I",
    "varchar" to "字符串型C",
    "char" to "字符串型C",
    "longtext" to "字符串型C",
    "text" to "字符串型C",
    "enum" to "字符串型C",
    "bit" to "字符串型C",
    "set" to "字符串型C",
    "json" to "字符串型C",
    "mediumtext" to "字符串型C",
)

val tableList = listOf<String>(
    "configcenter",
    "filecenter",
    "gateway",
    "messagecenter",
    "usercenter",
    "village_backpoor",
    "village_datacenter",
    "village_digital",
    "village_discuss",
    "village_monitor",
    "village_old",
    "village_party",
    "village_picture",
    "village_produce"
)

fun main() {
    val rows = ArrayList<Column>()
    for (table in tableList) {
        val list = Db.use().query("SELECT * FROM COLUMNS WHERE TABLE_SCHEMA='$table'")
        for (entity in list) {
            val type: String = DataType.get(entity.getStr("data_type")) ?: "字符串型C"
            rows.add(
                Column(
                    tableBianMa = entity.getStr("table_name") ?: "",
                    tableChinese = entity.getStr("column_comment") ?: "",
                    tableEng = entity.getStr("column_name") ?: "",
                    yeWuShuXing = if ((entity.getStr("column_name") ?: "").contains("id")) "非业务数据" else "业务数据",
                    laiYuan = "否",
                    type = type,
                    length = entity.getStr("character_maximum_length") ?: "",
                    isNull = if (StrUtil.equalsIgnoreCase("yes", entity.getStr("is_nullable"))) "是" else "否",
                    gongXiang = "有条件共享",
                    gongXiangTiaoJian = "工作参考业务协同",
                    gongXiangFenlei = "共享交换平台",
                    gongXiangFangShi = "接口",
                    isOpen = "否",
                    openTianJian = "",
                    noInSystem = "",
                    noGongXiang = "",
                    noOpen = "",
                    baseMsg = "其他",
                    muLu = "公共服务",
                    shuJuXiang = entity.getStr("column_comment") ?: "",
                    bianMa = "",
                    ziDian = "",
                )
            )
        }
    }
    outExcel(rows)

}


data class Column(
    val tableBianMa: String = "",
    val tableChinese: String = "",
    val tableEng: String = "",
    val yeWuShuXing: String = "业务数据",
    val laiYuan: String = "否",
    val type: String = "字符串",
    val length: String = "",
    val isNull: String = "是",
    val gongXiang: String = "有条件共享",
    val gongXiangTiaoJian: String = "工作参考业务协同",
    val gongXiangFenlei: String = "共享交换平台",
    val gongXiangFangShi: String = "接口",
    val isOpen: String = "否",
    val openTianJian: String = "",
    val noInSystem: String = "",
    val noGongXiang: String = "",
    val noOpen: String = "",
    val baseMsg: String = "其他",
    val muLu: String = "公共服务",
    val shuJuXiang: String = "",
    val bianMa: String = "",
    val ziDian: String = "",
)

fun outExcel(rows: ArrayList<Column>) {
    val writer = ExcelUtil.getWriter("${OutPath}所有字段的.xlsx")
    writer.addHeaderAlias("tableBianMa", "数据表编码（必填）")
    writer.addHeaderAlias("tableChinese", "数据项中文名称（必填）")
    writer.addHeaderAlias("tableEng", "数据项英文名称（必填）")
    writer.addHeaderAlias("yeWuShuXing", "业务属性（必填）")
    writer.addHeaderAlias("laiYuan", "是否来源于自有系统(0:否;1:是)选择否，不是来源于自有系统必填（必填）")
    writer.addHeaderAlias("type", "类型，填写字符串，长度是必填项（必填）")
    writer.addHeaderAlias("length", "长度")
    writer.addHeaderAlias("isNull", "是否可以为空(0：否  1：是)")
    writer.addHeaderAlias("gongXiang", "共享类型(1:无条件共享;2:有条件共享;3:不予共享)（必填）")
    writer.addHeaderAlias(
        "gongXiangTiaoJian",
        "共享条件(1:工作参考;2:工作参考业务协同;3:行政依据;4:履职共享;5:审批共享;6:业务协同;7:依申请公开)（必填）"
    )
    writer.addHeaderAlias("gongXiangFenlei", "共享方式分类(1:共享交换平台)（必填）")
    writer.addHeaderAlias("gongXiangFangShi", "共享方式类型(1:接口;2:数据库;3:文件;4:文件夹)（必填）")
    writer.addHeaderAlias("isOpen", "是否向社会开放(0:否;1:是)（必填）")
    writer.addHeaderAlias("openTianJian", "开放条件(1:无条件开放;2:依申请开放)")
    writer.addHeaderAlias("noInSystem", "不是来源于自由系统")
    writer.addHeaderAlias("noGongXiang", "不共享原因")
    writer.addHeaderAlias("noOpen", "不开放原因")
    writer.addHeaderAlias("baseMsg", "基础信息资源目录（备注：业务属性为业务数据时，该项必填）")
    writer.addHeaderAlias("muLu", "主题信息资源目录（备注：业务属性为业务数据时，该项必填）")
    writer.addHeaderAlias("shuJuXiang", "数据项描述")
    writer.addHeaderAlias("bianMa", "标签编码")
    writer.addHeaderAlias("ziDian", "数据字典编码")
    writer.write(rows, true)
    writer.close()
}